package com.swt.draw.controller;

import com.swt.draw.dao.LimitMapper;
import com.swt.draw.entity.Limit;
import com.swt.draw.example.LimitExample;
import com.swt.draw.vo.BaseResponse;
import com.swt.draw.vo.LayTableResponseVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author Administrator
 * @Date 2019/9/1 0001 11:38
 * @Version 1.0
 **/
@RestController
@RequestMapping("/limitManager")
public class LimitController {

    @Resource
    private LimitMapper limitMapper;

    /**
     * @Author ZPF
     * @Description 获取所有权限
     * @Date 11:39 2019/9/1 0001
     * @Param []
     * @return com.swt.draw.vo.BaseResponse
     **/
    @PostMapping("/getAllLimit")
    public LayTableResponseVO<Limit> getAllLimit(){
        LayTableResponseVO responseVO = new LayTableResponseVO();
        LimitExample example = new LimitExample();
        LimitExample.Criteria criteria = example.createCriteria();
        criteria.andLimitStatusNotEqualTo((short) 2);
        List<Limit> limits = limitMapper.selectByExample(example);
        Long count = limitMapper.countByExample(example);
        responseVO.setData(limits);
        responseVO.setCount(count);
        responseVO.setCode(1001);
        responseVO.setMsg("success");
        return responseVO;
    }

    /**
     * @Author ZPF
     * @Description 分页获取权限信息
     * @Date 14:02 2019/9/1 0001
     * @Param [page, limit]
     * @return com.swt.draw.vo.LayTableResponseVO<com.swt.draw.entity.Limit>
     **/
    @PostMapping("/getLimitList")
    public LayTableResponseVO<Limit> getLimitList(Integer page, Integer limit){
        LayTableResponseVO responseVO = new LayTableResponseVO();
        LimitExample example = new LimitExample();
        LimitExample.Criteria criteria = example.createCriteria();
        criteria.andLimitStatusNotEqualTo((short) 2);
        Long count = limitMapper.countByExample(example);
        responseVO.setCount(count);
        Integer start = (page - 1) * limit;
        Integer end = page * limit;
        List<Limit> limits = limitMapper.getLimitList(start, end);
        responseVO.setData(limits);
        responseVO.setCode(1001);
        responseVO.setMsg("success");
        return responseVO;
    }

    /**
     * @Author ZPF
     * @Description 跟新权限
     * @Date 14:39 2019/9/1 0001
     * @Param [limit]
     * @return com.swt.draw.vo.BaseResponse
     **/
    @RequestMapping("/updateLimit")
    public BaseResponse updateLimit(Limit limit){
        System.out.println(limit);
        BaseResponse response = new BaseResponse();
        LimitExample example = new LimitExample();
        LimitExample.Criteria criteria = example.createCriteria();
        criteria.andLimitNoEqualTo(limit.getLimitNo());
        limitMapper.updateByExampleSelective(limit, example);
        response.setCode(1001);
        response.setDesc("success");
        return response;
    }
}
